package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Carmoney;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class CarMoneyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Carmoney> carmoneyList;
    private Context mContext;


    public CarMoneyAdapter(List<Carmoney> list, Context context) {
        this.carmoneyList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (carmoneyList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_money_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(carmoneyList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (carmoneyList.size() > 0) {
            return carmoneyList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView numberTV;
        TextView statusLabelTV;
        TextView labelTV;
        TextView timeTV;
        TextView moneyTV;
        TextView remarkTV;
        TextView carnumberTV;

        public ViewHolder(View itemView) {
            super(itemView);
            numberTV = (TextView) itemView.findViewById(R.id.number);
            moneyTV = (TextView) itemView.findViewById(R.id.money);
            statusLabelTV = (TextView) itemView.findViewById(R.id.status_label);
            labelTV = (TextView) itemView.findViewById(R.id.label);
            timeTV = (TextView) itemView.findViewById(R.id.time);
            remarkTV = (TextView) itemView.findViewById(R.id.remark);
            carnumberTV = (TextView) itemView.findViewById(R.id.car_number);
        }

        public void BindViewData(Carmoney carmoney) {
            numberTV.setText(carmoney.getNumber());
            carnumberTV.setText((carmoney.getPlatenumbers()));
            if (carmoney.getType() <= 3) {
                statusLabelTV.setText("充");
                labelTV.setText("充值");
            } else {
                statusLabelTV.setText("取");
                labelTV.setText("取现");
            }
            timeTV.setText(TimeUtils.parseDateTime(carmoney.getAddtime()));
            moneyTV.setText("金额 " + carmoney.getMoney() + " 元");
            remarkTV.setText(carmoney.getAddusername() + "  " + carmoney.getRemark());
        }

    }
}
